1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
<script>
import { enhance } from "$app/forms";
import { send } from "$lib/socket";
import {
btc,
loc,
post,
copy,
f,
fail,
get,
types,
sat,
s,
sats,
} from "$lib/utils";
import { tick, onMount, onDestroy } from "svelte";
import { browser } from "$app/environment";
import { last, showQr, amountPrompt } from "$lib/store";
import Avatar from "$comp/Avatar.svelte";
import InvoiceData from "$comp/InvoiceData.svelte";
import InvoiceActions from "$comp/InvoiceActions.svelte";
import SetAmount from "$comp/SetAmount.svelte";
import SetMemo from "$comp/SetMemo.svelte";
import SetType from "$comp/SetType.svelte";
import { t } from "$lib/translations";
import { goto, invalidate } from "$app/navigation";
let { data } = $props();
let showOptions;
let reading = async ({ message, serialNumber }) => {
let name = serialNumber.replace(/:/g, "-");
let result = await post(`/fund/${name}/withdraw`, { amount, hash, name });
};
let readingerror = (e) => console.log("nfc error", e);
let { id, subject, user } = $state(data);
let { invoice, src } = $derived(data);
let { aid, amount, rate, tip, hash, text, type } = $derived(invoice);
let { memo } = $state(invoice);
let { username, currency } = $derived(invoice.user);
let locale = loc(user);
let tipPercent = $derived(tip ? (tip / amount) * 100 : 0);
// if (browser && !subbed[id])
// send("subscribe", invoice)
// .then(() => (subbed[id] = true))
// .catch((e) => {
// console.log("failed to subscribe to invoice notifications", invoice);
// console.log(e);
// });
let subbed = false;
$effect(() => {
if (!browser) return;
last.subscribe((v) => {
if (!v || subbed) return;
subbed = true;
send("subscribe", invoice);
});
});
onMount(async () => {
if (!browser) return;
if ($amountPrompt && !amount) toggleAmount();
});
let update = async (body) => {
try {
({ id } = await post(`/invoice`, {
invoice: body,
user: { username, currency },
}));
let url = `/invoice/${id}`;
if (subject?.prompt) url += "/tip";
else url += "?options=true";
goto(url, { invalidateAll: true, noScroll: true });
} catch (e) {
fail(e.message);
}
};
let settingType = $state();
let toggleType = () => (settingType = !settingType);
let setType = async (type, address_type) => {
$showQr = true;
if (type === types.lightning && !amount && typeof newAmount === "undefined")
goto(`/${username}/receive`, { invalidateAll: true, noScroll: true });
else {
// Build the POST body from a snapshot + explicit changes. Mutating the
// $derived `invoice` proxy and POSTing it serializes unreliably, leaking
// stale type/amount/hash from a prior interaction (see funk POS bug).
const body = { ...$state.snapshot(invoice), type, address_type };
if (typeof newAmount !== "undefined") body.amount = newAmount;
await update(body);
}
settingType = false;
};
let newAmount = $state(0);
let settingAmount = $state();
let toggleAmount = () => (settingAmount = !settingAmount);
let setAmount = async (e) => {
e?.preventDefault();
e?.stopPropagation();
settingAmount = false;
if (typeof $amountPrompt === "undefined") $amountPrompt = true;
const body = { ...$state.snapshot(invoice), amount: newAmount, tip: 0 };
await update(body);
};
let setMemo = async (e) => {
e.preventDefault();
e.stopPropagation();
settingMemo = false;
const body = { ...$state.snapshot(invoice), memo };
await update(body);
};
let settingMemo = $state();
let toggleMemo = () => (settingMemo = !settingMemo);
let fiat = $state(true);
let amountFiat = $derived(parseFloat(((amount * rate) / sats).toFixed(2)));
let tipAmount = $derived(((tip * rate) / sats).toFixed(2));
let link = $derived(
[types.bitcoin, types.liquid].includes(type) ? text : `lightning:${text}`,
);
let txt = $derived(
[types.bitcoin, types.liquid].includes(type) ? hash : text,
);
</script>
<div class="invoice container mx-auto max-w-xl px-4 space-y-2">
<InvoiceData
{src}
{link}
{txt}
{invoice}
{amount}
{amountFiat}
{currency}
{locale}
{tip}
{rate}
showQr={$showQr}
t={$t}
/>
<InvoiceActions
bind:newAmount
{setAmount}
{setType}
{toggleType}
{toggleAmount}
{toggleMemo}
{user}
{invoice}
{copy}
{link}
{type}
{txt}
t={$t}
bind:showQr={$showQr}
/>
</div>
<SetAmount
bind:newAmount
bind:fiat
{currency}
{locale}
{rate}
{settingAmount}
{setAmount}
{toggleAmount}
amountPrompt={$amountPrompt}
t={$t}
/>
<SetMemo bind:memo {settingMemo} {setMemo} {toggleMemo} t={$t} />
<SetType
bind:newAmount
{setAmount}
{invoice}
{user}
{type}
{settingType}
{setType}
{toggleType}
t={$t}
/>
|